function login() {
	var username = $('#login-username').val();
	var password = $('#login-password').val();
	$.ajax({
		type: "POST",
		url: "pages/login_p.php",
		data: $("#login-form").serialize(),
		success: function(data) {
			if(data == 800) {
				location.reload();
			} else {
				c_alert("Wrong username or password", "error");
			}
		}
	});
}

function register() {
	var firstName = $('#register-first-name').val();
	var lastName = $('#register-last-name').val();
	var email = $('#register-email').val();
	var username = $('#register-username').val();
	var password1 = $('#register-password1').val();
	var password2 = $('#register-password2').val();
	
	if(firstName == '' || lastName == '' || email == '' || username == '' || password1 == '' || password2 == '') {
		alert("Please ensure all fields are filled in");
		return 0;
	}
	if(password1 != password2) {
		alert("Your passwords do not match");
		return 1;
	}
	$.ajax({
		type: "POST",
		url: "pages/register_p.php",
		data: $("#register-form").serialize(),
		success: function(data) {
			if(data == 800) {
				document.location.href="index.php";
			}
			if(data == 1) {
				alert("Registration failed. Username already exists.");
			}
		}
	});
}

function add_portfolio() {
	var stockname = $('#stock_name').val();
	var stockprice = $('#stock_price').val();
	var optionprice = $('#option_price').val();
	var expiry = $('#expiry_time').val();
	var interest = $('#interest_rate').val();
	var volatility = $('#volatility').val();
	
	if(stockprice < 0 || optionprice < 0 || expiry < 0 || interest < 0 || volatility < 0) {
		alert("You cannot have negative values");
		return false;
	}
	else if(stockname != '' && stockprice != '' && optionprice != '' && expiry != '' && interest != '' && volatility != '') {
		$.ajax({
			type: "POST",
			url: "pages/portfolio_add_p.php",
			data: $("#calculator-form").serialize(),
			success: function(data) {
				if(data != false) {
					$('#stock-name').html(stockname);
					$('#stock-price').html(stockprice);
					$('#option-price').html(optionprice);
					$('#expiry').html(expiry);
					$('#interest').html(interest);
					$('#volatility').html(volatility);
					$('#final-result').html(Math.round(data *1000) / 1000);
					$('#calculator-results').fadeIn();
					$('#calculator-results td').fadeIn();
				}
			}
		});
	} else {
		alert("Empty fields are not allowed!");
	}
	
}

function add_portfolio_black() {
	var stockname = $('#stock_name').find('option:selected').html();
	var stockprice = $('#stock_price').val();
	var optionprice = $('#option_price').val();
	var expiry = $('#expiry_time').val();
	var interest = $('#interest_rate').val();
	var volatility = $('#volatility').val();
	
	if(stockprice < 0 || optionprice < 0 || expiry < 0 || interest < 0 || volatility < 0) {
		alert("You cannot have negative values");
		return false;
	}
	else if(stockname != '' && stockprice != '' && optionprice != '' && expiry != '' && interest != '' && volatility != '') {
		$.ajax({
			type: "POST",
			url: "pages/portfolio_add_black_p.php",
			data: $("#calculator-form").serialize(),
			success: function(data) {
				if(data != false) {
					$('#stock-name').html(stockname);
					$('#stock-price').html(stockprice);
					$('#option-price').html(optionprice);
					$('#expiry').html(expiry);
					$('#interest').html(interest);
					$('#volatility').html(volatility);
					$('#final-result').html(Math.round(data *1000) / 1000);
					$('#calculator-results').fadeIn();
					$('#calculator-results td').fadeIn();
				}
			}
		});
	} else {
		alert("Empty fields are not allowed!");
	}
}

function delete_portfolio(id) {
	$.ajax({
		type: "POST",
		url: "pages/portfolio_delete_p.php",
		data:({id: id}),
		success: function(data) {
			//alert(data);
			location.reload();
		}
	});
}

function delete_portfolio_black(id) {
	$.ajax({
		type: "POST",
		url: "pages/portfolio_delete_black_p.php",
		data:({id: id}),
		success: function(data) {
			//alert(data);
			location.reload();
		}
	});
}

function logout() {
	$.ajax({
		type: "POST",
		url: "pages/logout.php",
		success: function(data) {
			document.location.href="index.php";
		}
	});
}

function delete_user(id) {
	$('#' + id).slideUp();
	$.ajax({
		type: "POST",
		url: "pages/delete_users.php",
		data: ({id: id}),
	});
}

function search_user(name) {
	$.ajax({
		type: "POST",
		url: "pages/search_users.php",
		data: ({username: name}),
		success: function(data) {
			//error not working. alert(data);
		}
	});
}

function add_contact() {
	var message = $('#contact-message').val();
	var title = $('#contact-title').val();
	
	$.ajax({
		type: "POST",
		url: "pages/add_contact.php",
		data:({message: message, title: title}),
		success: function(data) {
			//alert(data);
			location.reload();
			//clear messages
		}
	});
}

function reply_inbox() {
	var ticket = $('#ticket-id').val();
	var message = $('#contact-message').val();
	$.ajax({
		type: "POST",
		url: "pages/inbox_reply_p.php",
		data:({message: message, ticket: ticket}),
		success: function(data) {
			//alert(data);
			location.reload();
		}
	});
}

function close_contact(id) {
	$.ajax({
		type: "POST",
		url: "pages/close_contact_p.php",
		data:({id: id}),
		success: function(data) {
			//alert(data);
			location.reload();
		}
	});
}

function switch_calculator() {
	$.ajax({
		type: "POST",
		url: "pages/switch_calculator_p.php",
		success: function(data) {
			location.reload();
		}
	});
}

function add_stock() {
	var name = $('#stock-name').val();
	var price = $('#stock-price').val();
	
	$.ajax({
		type: "POST",
		url: "pages/add_stock_p.php",
		data:({name: name, price: price}),
		success: function(data) {
			//alert(data);
			location.reload();
			//clear messages
		}
	});
}

function save_stock_changes(id) {
	var name = $('#editable-stock-name.class'+id).val();
	var price = $('#editable-stock-price.class'+id).val();
	$.ajax({
		type: "POST",
		url: "pages/edit_stock_p.php",
		data:({id: id, name: name, price: price}),
		success: function(data) {
			alert("Success");
		}
	});
}

function delete_stock(id) {
	$('tr.' + id).slideUp();
	$.ajax({
		type: "POST",
		url: "pages/delete_stocks_p.php",
		data: ({id: id}),
	});
}

function show_price() {
	id = $('#stock_name').val();
	$.ajax({
		type: "POST",
		url: "pages/get_stock_price.php",
		data:({id: id}),
		success: function(data) {
			$('#stock_price').val(data);
		}
	});
}

function c_alert(message, type) {
	var messageType;
	var containerType;
	var closeType;
	var spanType;
	switch (type) {
		case "error":
			containerType = "error-message-container ";
			spanType = "error-message-style";
			closeType = "error-close-button";
			break;
		case "warning":
			containerType = "warning-message-container";
			spanType = "warning-message-style";
			break;
		case "general":
			containerType = "general-message-container";
			spanType = "general-message-style";
			break;
		case "success":
			containerType = "success-message-container";
			spanType = "success-message-style";
			break;
	}
	$('body').append("<div id='overlay'><div id='message-container' class='" + containerType + "'><span id='" + spanType + "'>" + message + "</span><span id='message-close-button' class='" + closeType + "'>x</span></div></div>");
	$('body').delegate('#overlay', 'click', function() {
		$('#overlay').fadeOut();
	}).delegate('#message-container', 'click', function() {
		$('#overlay').fadeOut();
	});
}